﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DC2010.Tools;
using DC2010.Objects;
using DC2010.Objects.Info;
using DC2010;
using DC2010_Editor.Formulars;
using DC2010_Editor.Controls.Utils;
using DC2010_Editor.Properties;
using DC2010.Objects.Base;

namespace DC2010_Editor.Controls
{
    public partial class CreatureUC : UserControl
    {
        private Creature data { get; set; }

        public CreatureUC()
        {
            InitializeComponent();
        }

        private void CreatureUC_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        public void SetControls()
        {
            BasedOnTextBox.Text = data.Info.TemplateId;
            ItemIdTextBox.Text = data.Info.Id;

            textBox2.Text = data.Info.ShortName;
            textBox4.Text = data.Info.LongName;

            numericUpDown1.Value = (decimal)data.Info.Health;
            numericUpDown2.Value = (decimal)data.Info.HealthMax;
            numericUpDown42.Value = (decimal)data.Info.IsHealthBoosted;

            numericUpDown4.Value = (decimal)data.Info.Mana;
            numericUpDown3.Value = (decimal)data.Info.ManaMax;
            numericUpDown41.Value = (decimal)data.Info.IsManaBoosted;

            numericUpDown6.Value = (decimal)data.Info.Stamina;
            numericUpDown5.Value = (decimal)data.Info.StaminaMax;
            numericUpDown40.Value = (decimal)data.Info.IsStaminaBoosted;

            numericUpDown8.Value = (decimal)data.Info.Luck;
            numericUpDown7.Value = (decimal)data.Info.LuckMax;
            numericUpDown39.Value = (decimal)data.Info.IsLuckBoosted;

            numericUpDown14.Value = (decimal)data.Info.Strength;
            numericUpDown13.Value = (decimal)data.Info.StrengthMax;
            numericUpDown38.Value = (decimal)data.Info.IsStrengthBoosted;

            numericUpDown12.Value = (decimal)data.Info.Dexterity;
            numericUpDown11.Value = (decimal)data.Info.DexterityMax;
            numericUpDown37.Value = (decimal)data.Info.IsDexterityBoosted;

            numericUpDown10.Value = (decimal)data.Info.Vitality;
            numericUpDown9.Value = (decimal)data.Info.VitalityMax;
            numericUpDown36.Value = (decimal)data.Info.IsVitalityBoosted;

            numericUpDown16.Value = (decimal)data.Info.Intelligence;
            numericUpDown15.Value = (decimal)data.Info.IntelligenceMax;
            numericUpDown35.Value = (decimal)data.Info.IsIntelligenceBoosted;

            numericUpDown24.Value = (decimal)data.Info.AntiFire;
            numericUpDown23.Value = (decimal)data.Info.AntiFireMax;
            numericUpDown34.Value = (decimal)data.Info.IsAntiFireBoosted;

            numericUpDown22.Value = (decimal)data.Info.AntiWater;
            numericUpDown21.Value = (decimal)data.Info.AntiWaterMax;
            numericUpDown33.Value = (decimal)data.Info.IsAntiWaterBoosted;

            numericUpDown20.Value = (decimal)data.Info.AntiAir;
            numericUpDown19.Value = (decimal)data.Info.AntiAirMax;
            numericUpDown32.Value = (decimal)data.Info.IsAntiAirBoosted;

            numericUpDown18.Value = (decimal)data.Info.AntiEarth;
            numericUpDown17.Value = (decimal)data.Info.AntiEarthMax;
            numericUpDown31.Value = (decimal)data.Info.IsAntiEarthBoosted;

            numericUpDown28.Value = (decimal)data.Info.Food;
            numericUpDown27.Value = (decimal)data.Info.FoodMax;

            numericUpDown26.Value = (decimal)data.Info.Water;
            numericUpDown25.Value = (decimal)data.Info.WaterMax;

            checkBox1.Checked = data.Info.IsLevitating;
            checkBox2.Checked = data.Info.IsNonMaterial;
            checkBox3.Checked = data.Info.IsAbsorbingItems;
            checkBox4.Checked = data.Info.IsDead;
            checkBox5.Checked = data.Info.CanTeleport;
            checkBox6.Checked = data.Info.CanPassClosedDoors;
            checkBox7.Checked = data.Info.CanDematerialize;
            checkBox8.Checked = data.Info.HurtHead;
            checkBox9.Checked = data.Info.HurtTorso;
            checkBox10.Checked = data.Info.HurtLegs;
            checkBox11.Checked = data.Info.HurtFeet;
            checkBox12.Checked = data.Info.HurtRightHand;
            checkBox13.Checked = data.Info.HurtLeftHand;
            checkBox14.Checked = data.Info.HurtWeapon;

            textBox1.Text = data.Info.PartyId;

            inventoryTextBox2.SetRelatedObject(data.Info.Inventory.GetItems);
            inventoryTextBox3.SetRelatedObject(data.Info.Equip.Items.GetItems);
            inventoryTextBox4.SetRelatedObject(data.Info.Equip.Quiver.GetItems);
            inventoryTextBox5.SetRelatedObject(data.Info.Equip.Belt.GetItems);
            inventoryTextBox6.SetRelatedObject(data.Info.LinkedObjects);

            seTextBox3.SetRelatedObject(data.Info.Effects, EffectType.Pump);
            scTextBox1.SetRelatedObject(data.Info.Conditions);
        }

        public void GetFromControls()
        {
            data.Info.TemplateId = BasedOnTextBox.Text;
            data.Info.Id = ItemIdTextBox.Text;

            data.Info.ShortName = textBox2.Text;
            data.Info.LongName = textBox4.Text;

            data.Info.Health = (int)numericUpDown1.Value;
            data.Info.HealthMax = (int)numericUpDown2.Value;
            data.Info.IsHealthBoosted = (int)numericUpDown42.Value;

            data.Info.Mana = (int)numericUpDown4.Value;
            data.Info.ManaMax = (int)numericUpDown3.Value;
            data.Info.IsManaBoosted = (int)numericUpDown41.Value;

            data.Info.Stamina = (int)numericUpDown6.Value;
            data.Info.StaminaMax = (int)numericUpDown5.Value;
            data.Info.IsStaminaBoosted = (int)numericUpDown40.Value;

            data.Info.Luck = (int)numericUpDown8.Value;
            data.Info.LuckMax = (int)numericUpDown7.Value;
            data.Info.IsLuckBoosted = (int)numericUpDown39.Value;

            data.Info.Strength = (int)numericUpDown14.Value;
            data.Info.StrengthMax = (int)numericUpDown13.Value;
            data.Info.IsStrengthBoosted = (int)numericUpDown38.Value;

            data.Info.Dexterity = (int)numericUpDown12.Value;
            data.Info.DexterityMax = (int)numericUpDown11.Value;
            data.Info.IsDexterityBoosted = (int)numericUpDown37.Value;

            data.Info.Vitality = (int)numericUpDown10.Value;
            data.Info.VitalityMax = (int)numericUpDown9.Value;
            data.Info.IsVitalityBoosted = (int)numericUpDown36.Value;

            data.Info.Intelligence = (int)numericUpDown16.Value;
            data.Info.IntelligenceMax = (int)numericUpDown15.Value;
            data.Info.IsIntelligenceBoosted = (int)numericUpDown35.Value;

            data.Info.AntiFire = (int)numericUpDown24.Value;
            data.Info.AntiFireMax = (int)numericUpDown23.Value;
            data.Info.IsAntiFireBoosted = (int)numericUpDown34.Value;

            data.Info.AntiWater = (int)numericUpDown22.Value;
            data.Info.AntiWaterMax = (int)numericUpDown21.Value;
            data.Info.IsAntiWaterBoosted = (int)numericUpDown33.Value;

            data.Info.AntiAir = (int)numericUpDown20.Value;
            data.Info.AntiAirMax = (int)numericUpDown19.Value;
            data.Info.IsAntiAirBoosted = (int)numericUpDown32.Value;

            data.Info.AntiEarth = (int)numericUpDown18.Value;
            data.Info.AntiEarthMax = (int)numericUpDown17.Value;
            data.Info.IsAntiEarthBoosted = (int)numericUpDown31.Value;

            data.Info.Food = (int)numericUpDown28.Value;
            data.Info.FoodMax = (int)numericUpDown27.Value;

            data.Info.Water = (int)numericUpDown26.Value;
            data.Info.WaterMax = (int)numericUpDown25.Value;

            data.Info.IsLevitating = checkBox1.Checked;
            data.Info.IsNonMaterial = checkBox2.Checked;
            data.Info.IsAbsorbingItems = checkBox3.Checked;
            data.Info.IsDead = checkBox4.Checked;
            data.Info.CanTeleport = checkBox5.Checked;
            data.Info.CanPassClosedDoors = checkBox6.Checked;
            data.Info.CanDematerialize = checkBox7.Checked;
            data.Info.HurtHead = checkBox8.Checked;
            data.Info.HurtTorso = checkBox9.Checked;
            data.Info.HurtLegs = checkBox10.Checked;
            data.Info.HurtFeet = checkBox11.Checked;
            data.Info.HurtRightHand = checkBox12.Checked;
            data.Info.HurtLeftHand = checkBox13.Checked;
            data.Info.HurtWeapon =  checkBox14.Checked;

            data.Info.PartyId = textBox1.Text;

        }

        private void crystalButton4_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                CreatureGfxForm igfxf = new CreatureGfxForm(data.Gfx);
                igfxf.ShowDialog();
            }
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            Root.I.CreatureSystem.LoadInstance();
            Refresh();
        }

        private void DungeonImageListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBox ilb = (ImageListBox)sender;

            if (ilb.SelectedItem != null)
            {
                if (ilb.SelectedItem is ImageListBoxItem)
                {

                    ImageListBoxItem ilbi = (ImageListBoxItem)ilb.SelectedItem;

                    if (ilbi.Tag is Creature)
                    {
                        data = (Creature)ilbi.Tag;
                        SetControls();

                        if (string.IsNullOrEmpty(data.Info.TemplateId)) pictureBox1.Image = Resources.tick;
                        if (!string.IsNullOrEmpty(data.Info.TemplateId))
                        {
                            if (Root.I.CreatureSystem.Dict.ContainsKey(data.Info.TemplateId))
                                pictureBox1.Image = Resources.tick;
                            else
                                pictureBox1.Image = Resources.cross;
                        }
                    }
                }
            }
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            //noone was messing with that
            string id = string.Format(ItemIdTextBox.Text, Editor.I.CreaturesCount);
            string tempId = BasedOnTextBox.Text;

            Creature d = new Creature();
            d.Info.Id = id;
            d.Info.TemplateId = tempId;

            Root.I.CreatureSystem.AddToSystem(d, id, ObjectTarget.Core);
            Root.I.CreatureSystem.Save();

            Refresh();
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            ChooseBasedOn chbo = new ChooseBasedOn("Creature");
            chbo.ShowDialog();

            if (!string.IsNullOrEmpty(chbo.ChosenAncessor))
            {
                Creature i = Root.I.IS.GetCreature(chbo.ChosenAncessor);
                Creature iNew = (Creature)i.Clone();

                string id = string.Format(ItemIdTextBox.Text + "_{0}", Editor.I.CreaturesInstanceCount);
                string tempId = BasedOnTextBox.Text;


                iNew.Info.Id = id;
                iNew.Info.TemplateId = i.Info.Id;

                Root.I.CreatureSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
                Root.I.CreatureSystem.SaveInstance();

                Refresh();
            }
        }

        private void SaveFloorButton_Click(object sender, EventArgs e)
        {
            GetFromControls();

            Root.I.PartySystem.Save();
            Root.I.PartySystem.SaveInstance();

            Root.I.CreatureSystem.Save();
            Root.I.CreatureSystem.SaveInstance();

            Refresh();
        }

        private void Refresh()
        {
            TemplatesImageListBox.Items.Clear();
            DungeonImageListBox.Items.Clear();

            comboBox2.DataSource = null;
            comboBox2.Items.Clear();

            foreach (Creature i in Root.I.CreatureSystem.List)
            {
                ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Core, i);
                TemplatesImageListBox.Items.Add(itm);
            }

            foreach (Creature i in Root.I.CreatureSystem.ListInstance)
            {
                BaseObject c = i.FindOwner();
                if (c == null)
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Instanced, i);
                    DungeonImageListBox.Items.Add(itm);
                }
                else
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.InstancedWithOwner, i);
                    DungeonImageListBox.Items.Add(itm);
                }
            }

            comboBox2.DataSource = Enum.GetValues(typeof(Sex));
        }

        private void crystalButton5_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.CreatureSystem.MoveToC(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton6_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.CreatureSystem.MoveToI(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Info.Id;
                Root.I.CreatureSystem.RemoveFromSystem(data, ObjectTarget.Core);
                Root.I.CreatureSystem.RemoveFromSystem(data, ObjectTarget.Instance);
            }

            Refresh();
        }

        private void crystalButton8_Click(object sender, EventArgs e)
        {
            data.Id = data.Info.Id;
            Root.I.CreatureSystem.RemoveFromSystem(data, ObjectTarget.Instance);

            Creature i = Root.I.IS.GetCreature(data.Info.TemplateId);
            Creature iNew = (Creature)i.Clone();

            string id = data.Info.Id;
            string tempId = BasedOnTextBox.Text;

            iNew.Info.Id = id;
            iNew.Info.TemplateId = i.Info.Id;

            Root.I.CreatureSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
            Root.I.CreatureSystem.SaveInstance();

            Refresh();
        }

        private void crystalButton9_Click(object sender, EventArgs e)
        {
            string sPartyToCreate = textBox1.Text;

            Party p = new Party();
            p.Info.Id = sPartyToCreate;
            p.Info.TemplateId = sPartyToCreate;
            p.Id = sPartyToCreate;

            Root.I.PartySystem.AddToSystem(p, p.Info.Id, ObjectTarget.Instance);
            SaveFloorButton_Click(null, null);
        }

        private void crystalButton10_Click(object sender, EventArgs e)
        {
            string sPartyToInform = textBox1.Text;
            Party p  = Root.I.IS.GetParty(sPartyToInform);
            PartyInfoForm pif = new PartyInfoForm(p);
            pif.ShowDialog();
        }

        private void crystalButton11_Click(object sender, EventArgs e)
        {
            string sPartyToInform = textBox1.Text;
            Party p = Root.I.IS.GetParty(sPartyToInform);

            //if (p == null)
            //{
            //    string sPartyToCreate = textBox1.Text;

            //    p = new Party();
            //    p.Info.Id = sPartyToCreate;
            //    p.Info.TemplateId = sPartyToCreate;
            //    p.Id = sPartyToCreate;
            //}

            if (p != null)
            {

                p.JoinParty(data);

                Root.I.PartySystem.AddToSystem(p, p.Info.Id, ObjectTarget.Instance);
                SaveFloorButton_Click(null, null);
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string sPartyToInform = textBox1.Text;
            Party p = Root.I.IS.GetParty(sPartyToInform);

            crystalButton11.Visible = p != null;

        }


    }
}
